package com.anc.elasticsearch.utils;

import exception.AncException;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * 自定义断言类,抛出自定义异常
 *
 * @author Anc
 * @date 2021/7/8 下午 2:07
 * @since V1.0
 */
public class AssertUtils {

    private AssertUtils() {
        throw new IllegalStateException("Utility calss");
    }

    public static void hasText(@Nullable String text, String message) {
        if (!StringUtils.hasText(text)) {
            throw new AncException(message);
        }
    }

    public static void isNull(@Nullable Object object, String message) {
        if (object != null) {
            throw new AncException(message);
        }
    }

    public static void notNull(@Nullable Object object, String message) {
        if (object == null) {
            throw new AncException(message);
        }
    }

    public static void isEmpty(@Nullable Object[] array, String message) {
        if (!ObjectUtils.isEmpty(array)) {
            throw new AncException(message);
        }
    }

    public static void isEmpty(@Nullable Collection<?> collection, String message) {
        if (!CollectionUtils.isEmpty(collection)) {
            throw new AncException(message);
        }
    }

    public static void isEmpty(@Nullable Map<?, ?> map, String message) {
        if (!CollectionUtils.isEmpty(map)) {
            throw new AncException(message);
        }
    }

    public static void notEmpty(@Nullable Object[] array, String message) {
        if (ObjectUtils.isEmpty(array)) {
            throw new AncException(message);
        }
    }

    public static void notEmpty(@Nullable Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new AncException(message);
        }
    }

    public static void notEmpty(@Nullable Map<?, ?> map, String message) {
        if (CollectionUtils.isEmpty(map)) {
            throw new AncException(message);
        }
    }

    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new AncException(message);
        }
    }

    public static void notNull(@Nullable Object object,int code, String message) {
        if (Objects.isNull(object)) {
            throw new AncException(code,message);
        }
    }
}
